package oneD.t3dviewer;

/* JModelMenu.java  JComboBox specialized for tensegrity viewers

 History:

 06 Jun 2008  Created from ModelMenu.java substituting JComboBox for Choice.
 */

import javax.swing.*;
import java.io.*;
import java.util.HashMap;

/**
 * JComboBox specialized for tensegrity viewers. Menu options initialized by
 * data from an InputStream.
 * 
 * @author Bob Burkhardt
 */
public class JModelMenu extends JComboBox {

	/**
	 * Key is label from menu. Data is unqualified name of file containing model
	 * data.
	 */
	HashMap m_filenames = new HashMap();

	/**
	 * Create a menu from a stream of double quoted labels and filenames; format
	 * is label - filename - label - filename etc.
	 * 
	 * @param is
	 *            InputStream containing embedded double quoted labels and
	 *            filenames.
	 * @throws java.io.IOException
	 */
	public JModelMenu(InputStream is) throws IOException {
		super();
		Reader r = new BufferedReader(new InputStreamReader(is));
		StreamTokenizer st = new StreamTokenizer(r);
		st.commentChar('#');
		while (st.nextToken() != StreamTokenizer.TT_EOF) {
			// label
			if (st.ttype != '"')
				continue;
			String label = st.sval;
			// filename
			while (st.nextToken() != '"' && st.ttype != StreamTokenizer.TT_EOF)
				continue;
			if (st.ttype == StreamTokenizer.TT_EOF)
				break;
			m_filenames.put(label, st.sval);
			addItem(label);
		}
	}

	/**
	 * Give the filename String corresponding to the currently chosen menu item
	 * 
	 * @return The filename corresponding to the currently selected item.
	 */
	public String getSelectedFilename() {
		return (String) m_filenames.get(getSelectedItem());
	}

}
